<template>
  <div class="baseinfo" v-title data-title="代加工厂立项界面">
    <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol">
      <a-row :gutter="24">
        <a-col :span="8">
          <a-form-model-item label="项目编号" prop="projectNo" :label-col="{ span: 8 }" :wrapper-col="{ span: 15 }">
            {{form.projectNo}}
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item
            ref="projectName"
            label="项目名称"
            prop="projectName"
            :label-col="{ span: 8 }"
            :wrapper-col="{ span: 15 }"
          >
            <a-input
              v-model="form.projectName"
              @blur="
                () => {
                  $refs.projectName.onFieldBlur()
                }
              "
              placeholder="请输入有效的项目名称" 
              :disabled="form.status>1" 
            />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item
            label="项目分类"
            prop="projectTypeSubCategory"
            :label-col="{ span: 8 }"
            :wrapper-col="{ span: 15 }"
          >
            <a-select
              v-model="form.projectTypeSubCategory"
              placeholder="请选择项目分类"
              :options="projectCategoryOptions" :disabled="form.id!=''"  
            >
              <a-icon slot="suffixIcon" type="plus" />
            </a-select>
          </a-form-model-item>
        </a-col>
      </a-row>
      
      <a-row :gutter="24">
        <a-col :span="8">
          <a-form-model-item
            ref="mainDeptName"
            label="归属事业部"
            prop="mainDeptName"
            :label-col="{ span: 8 }"
            :wrapper-col="{ span: 15 }"
          >
            <a-input v-model="form.mainDeptName" placeholder="请填写正确的组织架构名称，系统自动校验" />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item
            label="产品粘贴商标"
            prop="pasteTrademark"
            :label-col="{ span: 8 }"
            :wrapper-col="{ span: 15 }"
          >
            <a-select
              v-model="form.pasteTrademark"
              placeholder="请选择产品粘贴商标"
              :options="pasteTrademarkOptions"
              @change="pasteTrademarkChange"
            >
              <a-icon slot="suffixIcon" type="plus" />
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item
            label="代加工类型"
            prop="processType"
            :label-col="{ span: 8 }"
            :wrapper-col="{ span: 15 }"
          >
            <a-select
              v-model="form.processType"
              placeholder="请选择代加工类型"
              :options="processTypeOptions"
              @change="processTypeChange"
            >
              <a-icon slot="suffixIcon" type="plus" />
            </a-select>
          </a-form-model-item>
        </a-col>
      </a-row>
      <!-- <a-row :gutter="24">
        <a-col :span="8">
          <a-form-model-item
            label="关联SRM业务系统链接"
            prop="srmSystemLink"
            :label-col="{ span: 8 }"
            :wrapper-col="{ span: 15 }"
          > -->
            <!-- <a-input v-model="form.srmSystemLink"  placeholder="点击选择关联系统" @click="associated.visible = true" /> -->
            <!-- <a-input v-model="form.srmSystemLink" placeholder="请输入关联SRM业务系统链接" />
          </a-form-model-item>
        </a-col> -->

        <!-- <a-col :span="8">
          <a-form-model-item
            label="链接名称"
            prop="srmSystemLinkName"
            :label-col="{ span: 8 }"
            :wrapper-col="{ span: 15 }"
          >
            <a-input v-model="form.srmSystemLinkName" placeholder="由关联系统自动带出" disabled/>
          </a-form-model-item>
        </a-col> -->
      <!-- </a-row> -->
      <a-row :gutter="24">
        <a-col :span="24">
          <a-form-model-item
            ref="srmSystemLinkName"
            prop="srmSystemLinkName"
            :label-col="{ span: 3 }"
            :wrapper-col="{ span: 20 }"
            class="slot-label"
          >
            <span slot="label">关联SRM业务<p>系统链接:</p></span>
            <a-textarea
              v-model="form.srmSystemLinkName"
              @blur="
                () => {
                  $refs.srmSystemLinkName.onFieldBlur()
                }
              "
              :auto-size="{ minRows: 3, maxRows: 5 }"
              style="width: 100%"
            />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="24">
          <a-form-model-item
            ref="agentFactoryName"
            label="代工厂名称"
            prop="agentFactoryName"
            :label-col="{ span: 3 }"
            :wrapper-col="{ span: 20 }"
          >
            <a-textarea
              v-model="form.agentFactoryName"
              @blur="
                () => {
                  $refs.agentFactoryName.onFieldBlur()
                }
              "
              :auto-size="{ minRows: 3, maxRows: 5 }"
              style="width: 100%"
            />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="24">
          <a-form-model-item
            ref="processProduct"
            label="代加工产品"
            prop="processProduct"
            :label-col="{ span: 3 }"
            :wrapper-col="{ span: 20 }"
          >
            <a-textarea
              v-model="form.processProduct"
              @blur="
                () => {
                  $refs.processProduct.onFieldBlur()
                }
              "
              :auto-size="{ minRows: 3, maxRows: 5 }"
              style="width: 100%"
            />
          </a-form-model-item>
        </a-col>
      </a-row>

      <a-row :gutter="24">
        <a-col :span="24">
          <a-form-model-item
            ref="processKeyword"
            label="关键字"
            prop="processKeyword"
            :label-col="{ span: 3 }"
            :wrapper-col="{ span: 20 }"
          >
            <a-textarea
              v-model="form.processKeyword"
              @blur="
                () => {
                  $refs.processKeyword.onFieldBlur()
                }
              "
              :auto-size="{ minRows: 3, maxRows: 5 }"
              style="width: 100%"
            />
          </a-form-model-item>
        </a-col>
      </a-row>
      <five-to-five ref="fiveForm" :projectBuildId="projectBuildId" :change-id="changeId" @addField="addFiveDataToForm"/>
    </a-form-model>
  
    <pagination-select
      :title="associated.title"
      :searchPlaceholder="associated.searchPlaceholder"
      :visible="associated.visible"
      :url="associated.url"
      :columns="associated.columns"
      @hatchHandleOk="associatedHandleOk"
    />

  </div>
</template>
<script>
import $ from 'jquery'
import { getAction, getActionAsync, postAction, postActionAsync } from '@/api/manage'
import Vue from 'vue'
export default {
  components: {
    PaginationSelect: () => import('@/components/tools/PaginationSelect'),
    FiveToFive: () => import('@/components/tools/projectBuildStup/baseInfo/FiveToFive')
  },
  props: {
    projectBuildId: {
      Type: String,
      default: '',
    },
    changeId: {
      Type: String,
      default: '',
    }
  },
  data() {
    return {
      labelCol: { span: 8 },
      wrapperCol: { span: 15 },
      form: {
        id:'',
        projectClass: '65',
        projectNo: '',
        projectName: '',
        mainDeptName: '',
        mainDeptId: '',
        projectTypeSubCategory: '',
        pasteTrademark: '',
        pasteTrademarkName: '',
        processType: '',
        processTypeName: '',
        srmSystemLink: '',
        srmSystemLinkName: '',
        agentFactoryName: '',
        processProduct: '',
        processKeyword: '', 
        status: 0 
      },
      rules: {
        projectName: [
          { required: true, message: '请输入有效的项目名', trigger: 'blur' },
          { min: 1, max: 50, message: '项目名不超过50个字符', trigger: 'blur' },
        ],
        mainDeptName: [
          { required: true, message: '请填写正确的组织架构名称，系统自动校验', trigger: 'blur' }
        ],
        projectTypeSubCategory: [{ required: true, message: '请选择项目分类', trigger: 'change' }],
        pasteTrademark: [{ required: true, message: '请选择产品粘贴商标', trigger: 'change' }],
        processType: [{ required: true, message: '请选择代加工类型', trigger: 'change' }],
        processProduct: [
          { required: true, message: '请输入代加工产品', trigger: 'blur' },
          { min: 1, max: 500, message: '代加工产品不超过500个字符', trigger: 'blur' },
        ],
        agentFactoryName: [
          { required: true, message: '请输入代工厂名称', trigger: 'blur' },
          { min: 1, max: 200, message: '代工厂名称不超过200个字符', trigger: 'blur' },
        ],
        processKeyword: [
          { required: true, message: '请输入关键字', trigger: 'blur' },
          { min: 1, max: 500, message: '关键字不超过500个字符', trigger: 'blur' },
        ],
        srmSystemLinkName: [
          { min: 1, max: 1000, message: '关联SRM业务系统链接不超过100个字符', trigger: 'blur' },
        ]
      },
      projectCategoryOptions: [],
      pasteTrademarkOptions: [],
      processTypeOptions: [],
      url: {
        codeInfoList: '/manage/codeInfo/list',
        getProjectByNo: '/projectBuild/getProjectByNo',
        save: '/projectBuild/add',
        queryVOById: '/projectBuild/queryVOById',
      },
      associated: {
        visible: false,
        title: '请选择关联项目',
        searchPlaceholder: '输入代码或名称查询',
        url: '/projectBuild/queryAssociated',
        columns: [
          {
            title: '关联项目编号',
            dataIndex: 'projectNo',
            align: 'center',
          },
          {
            title: '关联项目名称',
            dataIndex: 'projectName',
            align: 'center',
          }
        ],
      },
      loadFinish: false
    }
  },
  computed: {},
  methods: {
    addFiveDataToForm(val,valRef) {
      this.$set(this.form, valRef, val)
    },
    async doSubmit() {
      if(!this.loadFinish){
        layer.message.warning('请等待页面加载完毕')
        return {}  
      }
      var validR=false;
      try{
        validR = await this.$refs.ruleForm.validate();
        validR = await this.$refs.fiveForm.checkFiveWriteStatus()
      }catch(e){
        validR = false;
      }
      console.log('validR',validR);
      this.form.validResult = validR;
      let res=await this.doSave();
        this.projectBuildId=res;
      return {valid:validR,id:res};
    },
    async doSave() {
      let flag=null;
      this.form.changeId=this.changeId;
      var formData = JSON.parse(JSON.stringify(this.form));

      var vm = this
      await postActionAsync(this.url.save, formData)
        .then((res) => {
          if (res.code == 200) {
            vm.form.id = res.result.id;
            vm.form.projectNo = res.result.projectNo;
            this.$message.success('保存成功')
            return flag=res.result.id;
          } else {
            this.$message.warning('保存数据出错:' + res.message)
          }
        })
        .catch((res) => {
          this.$message.warning('保存数据出错:' + res.message)
        })
        return flag;
    },
    resetForm() {
      this.$refs.ruleForm.resetFields()
    },
    //初始化项目子类
    initProjectCategoryOptions() {
      var vm = this
      getAction(this.url.codeInfoList, { type: '项目子类', isdel: 0, parentid: vm.form.projectClass,pageSize:-1 })
        .then((res) => {
          if (res.code == 200) {
            var list = res.result
            if (list != null) {
              list.records.map((item) => {
                vm.projectCategoryOptions.push({ value: item.code, label: item.name })
              })

              if (vm.projectCategoryOptions.length === 1) {
                vm.form.projectTypeSubCategory = vm.projectCategoryOptions[0].value
              }
            }
          } else {
            this.$message.warning('查询项目子类失败:' + res.message)
          }
        })
        .catch((res) => {
          this.$message.warning('查询项目子类失败:' + res.message)
        })
    },
    initSelectOptions() {
      var vm = this
      getAction(this.url.codeInfoList, { type: '产品粘贴商标', pageNo:1,pageSize:9999,isdel: 0 })
        .then((res) => {
          if (res.code == 200) {
            var list = res.result
            if (list != null) {
              list.records.map((item) => {
                vm.pasteTrademarkOptions.push({ value: item.code, label: item.name })
              })
            }
          } else {
            this.$message.warning('查询产品粘贴商标失败:' + res.message)
          }
        })
        .catch((res) => {
          this.$message.warning('查询产品粘贴商标失败:' + res.message)
        })

        getAction(this.url.codeInfoList, { type: '代加工类型', pageNo:1,pageSize:9999,isdel: 0 })
        .then((res) => {
          if (res.code == 200) {
            var list = res.result
            if (list != null) {
              list.records.map((item) => {
                vm.processTypeOptions.push({ value: item.code, label: item.name })
              })
            }
          } else {
            this.$message.warning('查询代加工类型失败:' + res.message)
          }
        })
        .catch((res) => {
          this.$message.warning('查询代加工类型失败:' + res.message)
        })
    },
    pasteTrademarkChange (value,option) {
      this.form.pasteTrademarkName = option.componentOptions.children[0].text;
    },
    processTypeChange (value,option) {
      this.form.processTypeName = option.componentOptions.children[0].text;
    },
    associatedHandleOk(record) {
      if (record != null && record.length > 0) {
        this.form.srmSystemLink = record[0].projectNo
        this.form.srmSystemLinkName = record[0].projectName
      }
      this.associated.visible = false
    },
    //如果有id，则加载已填写信息
    initData() {
      if (this.projectBuildId != null && this.projectBuildId != '') {
        var vm = this
        getAction(this.url.queryVOById, { id: this.projectBuildId,changeId: this.changeId })
          .then((res) => {
            if (res.code == 200) {
              var result = res.result
              if (result != null) {
                this.form = result
                
                this.loadFinish = true
              } else {
                vm.$message.warning('初始化项目信息为空')
              }
            } else {
              this.$message.warning('初始化项目信息失败:' + res.message)
            }
          })
          .catch((res) => {
            this.$message.warning('初始化项目信息失败:' + res.message)
          })
      }else{
        this.loadFinish = true
      }
    },
  },
  mounted() {
    this.initProjectCategoryOptions()
    this.initSelectOptions()
    this.initData()
  },
}
</script>
<style lang="less" scoped>
/deep/ .ant-btn-primary {
  width: 85px;
  height: 30px;
  border-radius: 2px;
  background: #4095e4;
  margin-right: 10px;
}
/deep/ .default {
  width: 85px;
  height: 30px;
  border-radius: 2px;
}
.baseinfo {
  background-color: #fff;
  border: 1px solid #eaeaea;
  padding: 30px;
  .btn {
    display: flex;
    justify-content: center;
  }
}
</style>